package com.zc.im.service.im.group.impl;


import com.zc.im.mapper.im.group.ZcImGroupSignMapper;
import com.zc.im.model.im.group.ZcImGroupSign;
import com.zc.im.model.im.group.ZcImGroupSignExample;
import com.zc.im.model.im.group.ZcImGroupSignKey;
import com.zc.im.model.user.ZcComInfo;
import com.zc.im.service.im.group.ImGroupSignService;
import com.zc.im.service.user.ZcComInfoService;
import com.zc.im.util.DateUtils;
import com.zc.im.util.ImGolbalTypeContext;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 签到
 * Created by wangcc on 2017/7/8.
 */
@Service
public class ImGroupSignServiceImpl implements ImGroupSignService {


    @Autowired
    private ZcImGroupSignMapper zcImGroupSignMapper;
    @Autowired
    private ZcComInfoService zcComInfoService;
    private static Logger logger = Logger.getLogger(ImGroupSignServiceImpl.class);

    /**
     * 添加签到
     * @param zcImGroupSignModel
     * @return
     */
    @Override
    public Integer insertGroupSign(ZcImGroupSign zcImGroupSignModel) {
        Integer groupSign = null;
        if (zcImGroupSignModel == null) {
            logger.info("inset groupSign is null");
            return groupSign;
        }
        zcImGroupSignModel.setCreatedTime(new Date());
        zcImGroupSignModel.setStatus(ImGolbalTypeContext.STATUS_VALID);
        try {
            groupSign = zcImGroupSignMapper.insertSelective(zcImGroupSignModel);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupSign;
    }

    /**
     * 更新签到
     * @param zcImGroupSignModel
     * @return
     */
    @Override
    public Integer updateGroupSign(ZcImGroupSign zcImGroupSignModel) {
        Integer groupSign = null;
        if (zcImGroupSignModel == null) {
            logger.info("update groupSign is null");
            return groupSign;
        }
        zcImGroupSignModel.setUpdatedTime(new Date());
        try {
            ZcImGroupSignExample signExample = new ZcImGroupSignExample();
            ZcImGroupSignExample.Criteria criteria =  signExample.createCriteria();
            criteria.andIdEqualTo(zcImGroupSignModel.getId());

            groupSign = zcImGroupSignMapper.updateByExampleSelective(zcImGroupSignModel, signExample);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupSign;
    }

    /**
     * 删除签到
     * @param id
     * @return
     */
    @Override
    public Integer deleteGroupSign(Integer id) {
        Integer groupSign = null;
        if (id == null) {
            logger.info("delete groupSign is null");
            return groupSign;
        }
        try {
            ZcImGroupSignKey key = new ZcImGroupSignKey();
            key.setId(id);
            groupSign = zcImGroupSignMapper.deleteByPrimaryKey(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupSign;
    }

    /**
     * 根据公司id查询签到
     * @param zcImGroupSignModel
     * @return
     */
    @Override
    public List<ZcImGroupSign> queryAllGroupSign(ZcImGroupSign zcImGroupSignModel) {
        List<ZcImGroupSign> zcImGroupSignModels = new ArrayList<>();
        if (zcImGroupSignModel == null) {
            logger.info("queryAll   GroupSign is null");
            return zcImGroupSignModels;
        }
        try {
            ZcImGroupSignExample signExample = new ZcImGroupSignExample();
            ZcImGroupSignExample.Criteria criteria =  signExample.createCriteria();
            criteria.andCompanyIdEqualTo(zcImGroupSignModel.getCompanyId());

            zcImGroupSignModels = zcImGroupSignMapper.selectByExample(signExample);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return zcImGroupSignModels;
    }

    /**
     * 通过周月年检索迟到和早退
     * @param zcImGroupSign
     * @return
     */
    public List<ZcImGroupSign> listLateGroupSign(ZcImGroupSign zcImGroupSign){
        List<ZcImGroupSign> listzcImGroupSign = null;
        if (zcImGroupSign == null) {
            return listzcImGroupSign;
        }
        ZcImGroupSignExample example = new ZcImGroupSignExample();
        ZcImGroupSignExample.Criteria criteria = example.createCriteria();
        criteria.andSignTypeEqualTo(zcImGroupSign.getSignType());
        criteria.andCompanyIdEqualTo(zcImGroupSign.getCompanyId());
        criteria.andNormalSignStatusEqualTo(ImGolbalTypeContext.NORMAL_SIGN_STATUS_ABNORMAL);
        criteria.andCreatedTimeGreaterThanOrEqualTo(zcImGroupSign.getCreatedTime());
        criteria.andCreatedTimeLessThanOrEqualTo(zcImGroupSign.getUpdatedTime());
        try {
            listzcImGroupSign = zcImGroupSignMapper.selectByExample(example);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return listzcImGroupSign;
     }

    /**
     * 添加签到
     * @param zcImGroupSign
     * @return
     */
    @Override
    public Integer saveGroupSign(ZcImGroupSign zcImGroupSign) {
        Integer saveGroupSignKey = null;
        ZcImGroupSign zcImGroupSign1= new ZcImGroupSign();
        if (zcImGroupSign == null){
            return saveGroupSignKey;
        }
        if (zcImGroupSign.getSignType().equals("sign")) {
            saveGroupSignKey =  insertGroupSignSignOut(zcImGroupSign);
        } else if (zcImGroupSign.getSignType().equals("sign_out")) {
            saveGroupSignKey =  insertGroupSignEnd(zcImGroupSign);
        }

        return saveGroupSignKey;
    }

    /**
     * 处理上班时间
     * @param zcImGroupSignController
     * @return
     */
    private Integer insertGroupSignSignOut(ZcImGroupSign zcImGroupSignController) {
        Integer insertGroupSignStatusKey = null;
        if (zcImGroupSignController == null) {
            return insertGroupSignStatusKey;
        }
        ZcComInfo zcComInfo = new ZcComInfo();
        zcComInfo.setUid( zcImGroupSignController.getUserId() );
        ZcImGroupSign zcImGroupSign = new ZcImGroupSign();
        try {
            ZcComInfo zcComInfo1 = zcComInfoService.findworkTime(zcComInfo);
            Date presentTime = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String WorkShift =zcComInfo1.getMorningWorkTime()+" "+dateFormat.format(new Date());
            if (presentTime.getTime() > DateUtils.strFormatDateYmdHms(WorkShift).getTime()) {
                //添加签到时间  正常 ：normal
                zcImGroupSign.setNormalSignStatus(ImGolbalTypeContext.NORMAL_SIGN_STATUS_NORMAL);
                } else {
                //添加签到时间   非正常：abnormal'
                zcImGroupSign.setNormalSignStatus(ImGolbalTypeContext.NORMAL_SIGN_STATUS_ABNORMAL);
                }
                //添加正常的签到时间
                zcImGroupSign.setNormalSignTime(DateUtils.strFormatDateYmdHms( WorkShift ));
                //添加签到类型
                zcImGroupSign.setSignType(zcImGroupSignController.getSignType());
                //添加签到头像
                zcImGroupSign.setSignPics(zcImGroupSignController.getSignPics());
                //添加签到方式 用户签到user_sign
                zcImGroupSign.setSignMode(ImGolbalTypeContext.SIGN_MODE_USER_SIGN);
                //添加签到签到地址方式
                zcImGroupSign.setSignAddrModel(zcImGroupSignController.getSignAddrModel());
                //添加重建时间
                zcImGroupSign.setCreatedTime(presentTime);
                //添加签到内容
                zcImGroupSign.setSignContent(zcImGroupSignController.getSignContent());
                //添加用户id
                zcImGroupSign.setUserId(zcImGroupSignController.getUserId());
                //添加公司id
                zcImGroupSign.setCompanyId(zcComInfo1.getId());
                //添加群id
                zcImGroupSign.setGroupId(zcImGroupSignController.getGroupId());
                //添加状态
                zcImGroupSign.setStatus(ImGolbalTypeContext.STATUS_VALID);
                insertGroupSignStatusKey = insertGroupSign(zcImGroupSign);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return insertGroupSignStatusKey;
    }
    /**
     * 处理下班的时间
     * @param zcImGroupSignConterller
     * @return
     */
    private Integer insertGroupSignEnd(ZcImGroupSign zcImGroupSignConterller) {
        Integer insertGroupSignStatusKey = null;
        if (zcImGroupSignConterller == null) {
            return insertGroupSignStatusKey;
        }
        ZcComInfo zcComInfo = new ZcComInfo();
        zcComInfo.setUid(zcImGroupSignConterller.getUserId());
        ZcImGroupSign zcImGroupSign = new ZcImGroupSign();
        try {
          ZcComInfo zcComInfo1 = zcComInfoService.findworkTime(zcComInfo);
          Date time = new Date();
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          String ClosingTime = zcComInfo1.getAfternoonWorkTime()+" "+sdf.format(new Date());
          if (time.getTime() > DateUtils.strFormatDateYmdHms( ClosingTime ).getTime()) {
            //添加签到时间  正常 ：normal
            zcImGroupSign.setNormalSignStatus(ImGolbalTypeContext.NORMAL_SIGN_STATUS_NORMAL);
          } else {
            //添加签到时间   非正常：abnormal'
            zcImGroupSign.setNormalSignStatus(ImGolbalTypeContext.NORMAL_SIGN_STATUS_ABNORMAL);
          } //添加正常的签到时间
            zcImGroupSign.setNormalSignTime(DateUtils.strFormatDateYmdHms(ClosingTime));
            //添加签到类型
            zcImGroupSign.setSignType(zcImGroupSignConterller.getSignType());
            //添加签到头像
            zcImGroupSign.setSignPics(zcImGroupSignConterller.getSignPics());
            //添加签到方式 用户签到user_sign
            zcImGroupSign.setSignMode(ImGolbalTypeContext.SIGN_MODE_USER_SIGN);
            //添加签到签到地址方式
            zcImGroupSign.setSignAddrModel(zcImGroupSignConterller.getSignAddrModel());
            //添加重建时间
            zcImGroupSign.setCreatedTime(time);
            //添加签到内容
            zcImGroupSign.setSignContent(zcImGroupSignConterller.getSignContent());
            //添加用户id
            zcImGroupSign.setUserId(zcImGroupSignConterller.getUserId());
            //添加公司id
            zcImGroupSign.setCompanyId(zcComInfo1.getId());
            //添加群id
            zcImGroupSign.setGroupId(zcImGroupSignConterller.getGroupId());
            //添加状态
            zcImGroupSign.setStatus(ImGolbalTypeContext.STATUS_VALID);
            insertGroupSignStatusKey = insertGroupSign(zcImGroupSign);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return insertGroupSignStatusKey;
    }

    /**
     * 通过周月年检索迟到和早退
     * @param commpanyId
     * @param startTime
     * @param endTime
     * @param signType
     * @return
     */
    @Override
    public List<ZcImGroupSign> listLateGroupSign(Integer commpanyId, String startTime, String endTime, String signType) {
        List<ZcImGroupSign> listZcImGroupSign = null;
        if (commpanyId == null || signType==null || startTime==null|| endTime==null) {
            return listZcImGroupSign;
        }
        ZcImGroupSign zcImGroupSign = new ZcImGroupSign();
        zcImGroupSign.setCompanyId(commpanyId);
        zcImGroupSign.setCreatedTime(DateUtils.strFormatDateYmd(startTime));
        zcImGroupSign.setUpdatedTime(DateUtils.strFormatDateYmd(endTime));
        zcImGroupSign.setSignType(signType);
        try {
            listZcImGroupSign = listLateGroupSign(zcImGroupSign);
        }catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return listZcImGroupSign;
    }

}
